<?php

namespace addon\lian\model\core\settle;

use addon\lian\model\sqlmodel\LianRewardModel;
use addon\lian\model\sqlmodel\OrderModel;
use addon\shop\app\dict\order\OrderDict;
use addon\shop\app\model\order\Order;
use app\dict\order\OrderGoodsDict;
use core\base\BaseCoreService;

class CoreLianSettleAttrService
{   
    public $model;

    public function __construct()
    {
        $this->model = new LianRewardModel();
    }


    public function getRewardListAfterPay($reward_ids){
        $reward_list = (new LianRewardModel())->where('id', 'in', $reward_ids)->where('status', 1)->select();

        $pay_list = [];
        foreach($reward_list as $reward){
            $order_id = $reward['order_id']; 
            $order_info = (new OrderModel())->where('site_id', $reward['site_id'])
                ->where('order_id', $order_id)
                ->whereIn('order_status',[OrderGoodsDict::wait_delivery, OrderGoodsDict::delivery, OrderGoodsDict::delivery_finish])
                ->findOrEmpty(); 
            $reward['order_info'] = $order_info; 

            if(!$order_info->isEmpty()){ 
                $reward['pay_time'] = $order_info['pay_time'];    
                $pay_list[] = $reward; 
            }
        }

        return $pay_list; // 返回奖励列表，包含订单信息
    }


    public function getRewardListAfterFinish($reward_ids){
        $reward_list = $this->model->where('id', 'in', $reward_ids)->where('status', 1)->select();

        $finish_list = []; 
        foreach($reward_list as $reward){
            $order_id = $reward['order_id']; 
            $order_info = (new OrderModel())->where('site_id', $reward['site_id'])
                ->where('order_id', $order_id)
                ->where('order_status', OrderGoodsDict::delivery_finish)->findOrEmpty(); 
            $reward['order_info'] = $order_info; 

            if(!$order_info->isEmpty()){ 
                $reward['finish_time'] = $order_info['finish_time']; 
                $finish_list[] = $reward; 
            }
        }

        return $finish_list; 
    }
}